﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using DevExpress.LocalizationCHS;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;

using DevExpress.XtraEditors.Repository;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using JsalesModel;


namespace JSales.ModuleProduct
{
    public partial class GrammageSetForm : Form
    {
        public GrammageSetForm()
        {
            InitializeComponent();
        }

        private BindingList<TbGrammage> grammage=new BindingList<TbGrammage>();
        private void CtrolINI()
        {
            barButtonAdd.Enabled = true;
            barButtonSave.Enabled = false;

            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
        }

        private void grammage_AddingNew(object sender, AddingNewEventArgs e)
        {
            //e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
            e.NewObject = grammage.AddNew();
        }

        private void grammage_ListChanged(object sender, ListChangedEventArgs e)
        {

        }

        private void GrammageSetForm_Load(object sender, EventArgs e)
        {
        //汉化界面
        //    DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
        //    DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active= new DevExpressXtraTreeListLocalizationCHS();
            
            CtrolINI();

            grammage.AddingNew+=new AddingNewEventHandler(grammage_AddingNew);
            grammage.ListChanged += new ListChangedEventHandler(grammage_ListChanged);

            grammage.Clear();
            foreach (TbGrammage G in GlobalVar.GGrammages)
            {
                grammage.Add(G);
            }

            gridControl1.DataSource = grammage;
            //GlobalVar.GGrammages.ToList();
            
            //一定要转换为BindingList<>对象绑定，才能触发GridView的InitNewRow事件。

            gridView1.Columns["GRAMMAGEID"].FieldName = "GRAMMAGEID";
            gridView1.Columns["GRAMMAGEID"].Caption = "克重[g/m²]";
            gridView1.Columns["GRAMMAGEID"].VisibleIndex = 0;
            
            gridView1.Columns["GRAMMAGENAME"].FieldName = "GRAMMAGENAME";
            gridView1.Columns["GRAMMAGENAME"].Caption = "克重说明";
            gridView1.Columns["GRAMMAGENAME"].VisibleIndex = 1;

            gridView1.Columns["ISDELETED"].FieldName = "ISDELETED";
            gridView1.Columns["ISDELETED"].Caption = "停用标志";
            gridView1.Columns["ISDELETED"].VisibleIndex = 2;

            CtrolINI();
        }
        
        private void GridView1InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            view.SetRowCellValue(e.RowHandle, view.Columns["GRAMMAGEID"], null);
            view.SetRowCellValue(e.RowHandle, view.Columns["GRAMMAGENAME"], null);
            this.gridView1.UpdateCurrentRow();
            //var user = new TbGrammage();
        }
        
        private void BarButtonAddItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
            gridView1.OptionsView.NewItemRowPosition=NewItemRowPosition.Top;
            barButtonSave.Enabled = true;
        }

        private void barButtonSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(GlobalVar.GGrammages.Any())
            {
//                foreach (DataRow row in GlobalVar.GlobaDs.Tables["Grammage"].Rows)
//                {
//                    switch (row.RowState)
//                    {
//                        case DataRowState.Added:
//                            TbGrammage Grammage_a = new TbGrammage();
//                            Grammage_a.Grammageid = int.Parse(row["GRAMMAGEID"].ToString());
//                            Grammage_a.Grammagename = row["GRAMMAGENAME"].ToString();
//                            Grammage_a.Isdeleted = false;
//                            GrammageClass.Create(Grammage_a);
//                            this.GrammageSetForm_Load(sender, e);
//                            break;
//                        case DataRowState.Deleted:
//                            TbGrammage Grammage_d = GrammageClass.Query(int.Parse(row["GRAMMAGEID"].ToString()));
//                            Grammage_d.Isdeleted = true;
//                            GrammageClass.Update(Grammage_d);
//                            this.GrammageSetForm_Load(sender, e);
//                            break;
//                        case DataRowState.Modified:
//                            TbGrammage Grammage_m = GrammageClass.Query(int.Parse(row["GRAMMAGEID"].ToString()));
//                            Grammage_m.Grammageid = int.Parse(row["GRAMMAGEID"].ToString());
//                            Grammage_m.Grammagename = row["GRAMMAGENAME"].ToString();
//                            Grammage_m.Isdeleted = bool.Parse(row["Isdeleted"].ToString());
//                            GrammageClass.Update(Grammage_m);
//                            this.GrammageSetForm_Load(sender, e);
//                            break;
//                        case DataRowState.Unchanged:
//                            //MessageBox.Show("Unchanged");
//                            break;
//                    }
//                } 
//                GlobalVar.GlobaDs.Tables["Grammage"].AcceptChanges();
            }

        }

        private void barButtonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            barButtonSave.Enabled = true;
        }

        private void barButtonCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl1.FocusedView.CloseEditor();
            CtrolINI();
        }
        
        private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;
            GridColumn IDColumn = view.Columns["GRAMMAGEID"];
            if (view.GetRowCellValue(e.RowHandle, IDColumn).Equals(DBNull.Value) || view.GetRowCellValue(e.RowHandle, IDColumn).Equals(null))
            {
                e.Valid = false;
                view.SetColumnError(IDColumn, "克重不能为空！");
            }
            if (view.GetRowCellValue(e.RowHandle, IDColumn).ToString().Length>3)
            {
                e.Valid = false;
                view.SetColumnError(IDColumn, "克重长度不能大于3！");
            }   
            



            //else
            //{
            //    if (view.GetRowCellValue(e.RowHandle, IDColumn).ToString().Length > 3)
            //    {
            //        e.Valid = false;
            //        view.SetColumnError(IDColumn, "ID长度超过3位");
            //    }
            //}
            
            
            /*
            DataRowView focusedRow = (DataRowView)gridView1.GetFocusedRow();
            if (!focusedRow.IsNew) return;
            foreach (DataColumn column in focusedRow.DataView.Table.Columns)
            {
                e.Valid = !focusedRow.Row.IsNull(column);
                if (!e.Valid) return;
            }
            */

        }

        private void gridView1_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
            //另一种异常模式
            //e.ExceptionMode = ExceptionMode.DisplayError;
            //e.WindowCaption = "输入错误！";
        }

        private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }
    }
}
